using System;
namespace APP.HELPER.Utils.BarcodeLib
{
	internal class ITF14 : BarcodeCommon, IBarcode
	{
		private string[] ITF14_Code = new string[]
		{
			"NNWWN",
			"WNNNW",
			"NWNNW",
			"WWNNN",
			"NNWNW",
			"WNWNN",
			"NWWNN",
			"NNNWW",
			"WNNWN",
			"NWNWN"
		};
		public string Encoded_Value
		{
			get
			{
				return this.Encode_ITF14();
			}
		}
		public ITF14(string input)
		{
			this.Raw_Data = input;
			this.CheckDigit();
		}
		private string Encode_ITF14()
		{
			if (this.Raw_Data.Length > 14 || this.Raw_Data.Length < 13)
			{
				throw new Exception("EITF14-1: Data length invalid. (Length must be 13 or 14)");
			}
			if (!BarcodeEdit.CheckNumericOnly(this.Raw_Data))
			{
				throw new Exception("EITF14-2: Numeric data only.");
			}
			string str = "1010";
			for (int i = 0; i < this.Raw_Data.Length; i += 2)
			{
				bool flag = true;
				string text = this.ITF14_Code[int.Parse(this.Raw_Data[i].ToString())];
				string text2 = this.ITF14_Code[int.Parse(this.Raw_Data[i + 1].ToString())];
				string text3 = "";
				while (text.Length > 0)
				{
					text3 = text3 + text[0].ToString() + text2[0].ToString();
					text = text.Substring(1);
					text2 = text2.Substring(1);
				}
				string text4 = text3;
				for (int j = 0; j < text4.Length; j++)
				{
					char c = text4[j];
					if (flag)
					{
						if (c == 'N')
						{
							str += "1";
						}
						else
						{
							str += "11";
						}
					}
					else
					{
						if (c == 'N')
						{
							str += "0";
						}
						else
						{
							str += "00";
						}
					}
					flag = !flag;
				}
			}
			return str + "1101";
		}
		private void CheckDigit()
		{
			if (this.Raw_Data.Length == 13)
			{
				int num = 0;
				int num2 = 0;
				for (int i = 0; i <= 10; i += 2)
				{
					num2 += int.Parse(this.Raw_Data.Substring(i, 1));
				}
				for (int i = 1; i <= 11; i += 2)
				{
					num += int.Parse(this.Raw_Data.Substring(i, 1)) * 3;
				}
				int num3 = num + num2;
				int num4 = num3 % 10;
				num4 = 10 - num4;
				if (num4 == 10)
				{
					num4 = 0;
				}
				this.Raw_Data += num4.ToString();
			}
		}
	}
}
